$(function () {
    getMenuTreeData();
});
function loadMenuTree(menuTree) {
    $('#menuTree').jstree({
        "plugins" : [ "wholerow"],
        'core' : {
            'data' : menuTree
        },
        "checkbox" : {
            //"keep_selected_style" : false,
            //"undetermined" : true
            //"three_state" : false,
            //"cascade" : ' up'
        }
    });
    $('#menuTree').jstree('open_all');
    $('#menuTree').bind("activate_node.jstree", function (obj, e) {
        let currentNode = e.node;
        doSelectNode(currentNode.id)
    });
}
function doSelectNode(categoryId){
    $('#menuTree').jstree('deselect_node',categoryId);
    if(categoryId==-1){
        $('#menuTree').jstree('select_node',-1);
        $("#searchCategoryId").val(null)
        reLoad();
        return;
    }
    let d=$('#menuTree').jstree('get_node',categoryId);
    if(d.children.length==0){
        $('#menuTree').jstree('select_node',d.id);
        $("#searchCategoryId").val(d.id)
        reLoad();
        return ;
    }else{
        doSelectNode(d.children[0])
    }

}
function getMenuTreeData() {
    var productId = $('#productId').val();
    $.ajax({
        type : "GET",
        url : "/shop/productInCategory/tree/" + productId,
        success : function(data) {
            loadMenuTree(data);
        }
    });
}